package com.aidex.biz.controller;

import java.util.List;

import com.aidex.biz.domain.Customer;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.CustomerTest;
import com.aidex.biz.service.CustomerTestService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 基础Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-05-23
 */
@RestController
@RequestMapping("/biz/bizCustomerTest")
public class CustomerTestController extends BaseController {
    @Autowired
    private CustomerTestService customerTestService;

    /**
     * 查询基础列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerTest:list')")
    @GetMapping("/list")
    public R<PageInfo> list(CustomerTest customerTest, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerTest.setCompanyId(null);
        } else {
            customerTest.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        customerTest.setPage(new PageDomain(request, response));
        return R.data(customerTestService.findPage(customerTest));
    }

    /**
     * 获取基础详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerTest:query')")
    @GetMapping(value = "/{id}")
    public R<CustomerTest> detail(@PathVariable("id") String id) {
        return R.data(customerTestService.get(id));
    }

    /**
     * 新增基础
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerTest:add')")
    @Log(title = "基础", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated CustomerTest customerTest) {
        return R.status(customerTestService.save(customerTest));
    }

    /**
     * 修改基础
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerTest:edit')")
    @Log(title = "基础", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CustomerTest customerTest) {
        return R.status(customerTestService.save(customerTest));
    }


    /**
     * 删除基础
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerTest:remove')")
    @Log(title = "基础", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(customerTestService.deleteCustomerTestByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerTest:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(customerTestService.findMaxSort(new CustomerTest()));
    }

    /**
     * 导出基础列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerTest:export')")
    @Log(title = "基础", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CustomerTest customerTest) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerTest.setCompanyId(null);
        } else {
            customerTest.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<CustomerTest> list = customerTestService.findList(customerTest);
        ExcelUtil<CustomerTest> util = new ExcelUtil<CustomerTest>(CustomerTest.class);
        return util.exportExcel(list, "基础数据");
    }

    /**
     * 查询基础列表
     */
    @GetMapping("/joinList")
    @DataScope(userAlias = "a", deptAlias = "a")
    public R<PageInfo> joinList(Customer customerTest, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerTest.setCompanyId(null);
        } else {
            customerTest.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }

        customerTest.setPage(new PageDomain(request, response));
        return R.data(customerTestService.findCustomerTestJoinList(customerTest));
    }

}
